
#include "polymerization.h"
#include <iostream>
#include <cmath>
#include <cstdarg>
using std::cin;
using std::cout;
using std::endl;


node::node()
{
    m_order=m_coefficient=0;
    m_next=NULL;
}

node::~node()
{

}

int node::getorder() const
{
    return m_order;
}

void node::setorder(int rorder)
{
    m_order=rorder;
}

int node::getcoefficient() const
{
    return m_coefficient;
}

void node::setcoefficient(int rcoefficient)
{
    m_coefficient=rcoefficient;
}

node* node::getnext() const
{
    return m_next;
}

void node::setnext(node * rnext)
{
        if (rnext==NULL)
        m_next=NULL;
        else
        m_next=rnext;
}

polymerization::polymerization()
{
    m_first=NULL;
    m_tial=NULL;
}

polymerization::polymerization(polymerization * temp)
{
    m_first=temp->getfirst();
    m_tial=temp->gettial();

}
polymerization::~polymerization()
{
    node * p=m_first;
    while(m_first)
    {
        m_first=m_first->getnext();
        delete p;
    }
}

void polymerization::addend(node * temp)
{
    if(!m_first)
    {
        m_first=temp;
        temp->setnext(NULL);
        m_tial=temp;

    }
    else
    {
        m_tial->setnext(temp);
        m_tial=temp;
        m_tial->setnext(NULL);
    }
}
void polymerization::copytoend(node * temp)
{
    node * tempnode=new node;
    tempnode->setorder(temp->getorder());
    tempnode->setcoefficient(temp->getcoefficient());
    addend(tempnode);
}
void polymerization::setpoly(int first,...)
{
    int count=1;

    node * p=m_first;
    while(m_first)
    {
        m_first=m_first->getnext();
        delete p;
    }
    m_first=m_tial=NULL;


    va_list inputlist;
    va_start(inputlist,first);


    double input;
    node *temp;
    int flag=0;
    while (count<=first)  //there are others number should input
    {
        if(!flag)
        {
            input=va_arg(inputlist,double);
        }
        else
        {
            input=va_arg(inputlist,int);
        }
        if(!flag)
        {
            temp =new node;
            flag=1;
            temp->setcoefficient(input);
        }
        else
        {
            flag=0;
            temp->setorder(input);
            addend(temp);
        }
        count++;
    }
    temp=NULL;
    va_end(inputlist);
}

node * polymerization::getfirst() const
{
    return m_first;
}

node * polymerization::gettial() const
{
    return m_tial;
}
polymerization * polymerization::operator+(const polymerization &addpoly)  const
{
    polymerization * temp=new polymerization;
    node * q=addpoly.getfirst();
    node * p=m_first;
    while(p&&q)
    {


        if(p->getorder()>q->getorder())
        {
            temp->copytoend(p);
            p=p->getnext();
        }
        else if(p->getorder()==q->getorder())
        {
            node * tempnode=new node;
            tempnode->setorder(p->getorder());
            tempnode->setcoefficient(p->getcoefficient()+q->getcoefficient());
            temp->addend(tempnode);
            p=p->getnext();
            q=q->getnext();
        }
        else
        {
            temp->copytoend(q);
            q=q->getnext();
        }
    }
    if(q)
        p=q;
    while(p)
    {
        temp->copytoend(p);
        p=p->getnext();
    }
    return temp;
}


void polymerization::operator=(polymerization * temp)
{
    node * p=m_first;
    while(m_first)
    {
        m_first=m_first->getnext();
        delete p;
    }
    m_first=temp->getfirst();

}


polymerization * polymerization::operator*( const polymerization & timepoly) const
{
    polymerization * temppoly=new polymerization,* result=new polymerization;
    node * p=m_first,*q=timepoly.getfirst(),*tempnode;

    while(p)
    {
        while(q)
        {
            tempnode=new node;
            tempnode->setcoefficient(p->getcoefficient()*q->getcoefficient());
            tempnode->setorder(p->getorder()+q->getorder());
            temppoly->addend(tempnode);
            q=q->getnext();
        }
        p=p->getnext();

        q=timepoly.getfirst();
        result=*result+*temppoly;
        delete temppoly;
        temppoly=new polymerization;
    }
    return result;
}


double polymerization::substitute(double  x)const
{
    double result=0;
    node * temp=m_first;
    while(temp)
    {
        result+=(temp->getcoefficient()*pow(x,temp->getorder()));
        temp=temp->getnext();
    }
    return result;

}


void polymerization::show()
{
    cout<<"the polymerization is "<<endl;
    node * temp=m_first;
    while(temp)
    {
        cout<<temp->getcoefficient()<<"X^"<<temp->getorder();
        temp=temp->getnext();
        if(temp)
        {
            cout<<'+';
        }
    }
    cout<<endl;
}
